{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 系统基本信息检查"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 28,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'hostName': 'IQSZ-L01842',\n",
       " 'osName': 'nt',\n",
       " 'userName': 'BIANHAIYUAN198',\n",
       " 'hostIp': '10.118.155.202'}"
      ]
     },
     "execution_count": 28,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 获取母系统的hostname，ip, admin，os\n",
    "\n",
    "import os\n",
    "import socket\n",
    "\n",
    "envInfo = {\"hostName\":socket.gethostname(),\"osName\":os.name,\"userName\":os.environ['USERNAME'],\"hostIp\":socket.gethostbyname(socket.gethostname()) }\n",
    "envInfo"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "/usr/sbin/mysqld\n"
     ]
    }
   ],
   "source": [
    "# 检查母系统是否安装某应用\n",
    "\n",
    "import psutil\n",
    "# serviceName = \"MySQL\"\n",
    "# serviceName = \"mysqlrouter\"\n",
    "serviceName = \"mysqld\"\n",
    "def assert_app_installed(serviceName):\n",
    "    processList = [proc for proc in psutil.process_iter(attrs=['pid', 'name']) if serviceName in proc.info[\"name\"]]\n",
    "    try:\n",
    "        if len(processList) >= 1:\n",
    "            gones, alives = psutil.wait_procs(processList,timeout=3)\n",
    "            return alives[0].exe()\n",
    "    except PermissionError:\n",
    "        pass\n",
    "    return processList\n",
    "# alives[pid]\n",
    "print(assert_app_installed(\"mysqld\"))\n",
    "# processList,processList[0].name(),processList[0].status(), processList[0].connections(),processList[0].cwd()\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 101,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'D:\\\\Users\\\\bianhaiyuan198\\\\DevelopProject\\\\gulf_project'"
      ]
     },
     "execution_count": 101,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 查看当前执行代码的路径\n",
    "import inspect, os\n",
    "inspect.getfile(inspect.currentframe()) # script filename (usually with path)\n",
    "os.path.dirname(os.path.abspath(inspect.getfile(inspect.currentframe()))) # script directory"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 112,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "<_sre.SRE_Match object; span=(0, 33), match='paSmartcity_SMT_SZ_iOS_infer_test'>"
      ]
     },
     "execution_count": 112,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "import re\n",
    "pt = re.compile(\"\\S+[_]infer[_]\\S+\")\n",
    "sd = \"paSmartcity_SMT_SZ_iOS_infer_test\"\n",
    "pt.match(sd)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "izwz90ajic0vbw7o4pj6xyz\n"
     ]
    }
   ],
   "source": [
    "## 使用python socket.gethostname() 获取系统的hostname\n",
    "# Use socket and its gethostname() functionality. This will get the hostname of the computer where the Python interpreter is running:\n",
    "# ```````````````````````````````````````````````````````````````````````````````````````````````````````````````````````````````````\n",
    "import socket\n",
    "print(socket.gethostname())\n",
    "\n",
    "\n",
    "## 注意文件系统编码，和特殊的格式要求\n",
    "# 在编码前，确认配置IDE（eclipse，操作系统的文本编码为 UTF-8，或GBK\n",
    "# Jenkins 环境变量或构建参数 赋值不能有空格"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'13939528473'"
      ]
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "## python 随机生成电话号码\n",
    "import random\n",
    "random.choice(['139','188','185','136','158','151'])+\"\".join(random.choice(\"0123456789\") for i in range(8))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'x': 1, 'y': 2}"
      ]
     },
     "execution_count": 2,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "## python 将字符串列表或字典 转换未 列表或字典\n",
    "\n",
    "import ast\n",
    "ast.literal_eval(\"{'x':1, 'y':2}\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.8"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
